<?php
namespace app\classroom\logic;
use think\Db;

class LearnLogic extends BaseLogic {

    /**
     * 检查开始学习规则
     * @param $openid
     * @param $clid
     * @return mixed
     */
    public function check_start_leran_rule($openid,$clid){
        //处理用户超时的次数
        $cl = new \app\common\controller\ClassLearn();
        $r = $cl->deal_outtime_ci_by_user($openid,$clid);

        //检查活动是否存在
        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps);
        if(empty($cl_detail)){
            return $this->get_response(0,'该学习不存在');
        }
        $now_time = date('Y-m-d H:i:s');
        if($cl_detail['start_time'] > $now_time || $cl_detail['end_time'] < $now_time){
            return $this->get_response(0,'该学习未开始或已结束');
        }

        //获取当天已经学习多少次
        $today_time = get_today_time();
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $maps[] = ['start_time','between',[$today_time['start_time'],$today_time['end_time']]];
        $today_ci_num = model('class_learn_ci')->get_count($maps,'id');

        //获取最后一次学习次
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps,'id,start_time,end_time,out_time','id desc');

        //0，代表不能开始学习
        //1，代表开始新的学习
        //2，代表继续上次学习
        //检查是否有正在进行中的学习
        if($clc_detail['start_time'] < $now_time && $clc_detail['out_time'] > $now_time && $clc_detail['end_time'] == '0000-00-00 00:00:00'){
            $day_date = date('Y-m-d');

            //如果没有跨天，继续上次学习
            if($day_date == date('Y-m-d',strtotime($clc_detail['start_time']))){
                $data['clcid'] = $clc_detail['id'];
                return $this->get_response(2,'继续上次学习',$data);
            }
            //如果有跨天，并且系统不允许跨天，那么不能继续学习，走下一个流程
        }

        //判断今天学习次数有没有用完
        if($today_ci_num < $cl_detail['per_ci_num']){
            return $this->get_response(1,'开始新的学习');
        }
        return $this->get_response(0,'今天学习次数已用完');
    }

    /**
     * 开始新的学习
     * @param $openid
     * @param $clid
     * @return mixed
     */
    public function start_new_learn($openid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps,'dati_time');

        //新增学习次数
        $clc_data = [];
        $clc_data['openid'] = $openid;
        $clc_data['clid'] = $clid;
        $clc_data['start_time'] = date('Y-m-d H:i:s');
        $clc_data['out_time'] = date('Y-m-d H:i:s',strtotime('+' . $cl_detail['dati_time'] . ' minute'));
        $clcid = model('class_learn_ci')->insertGetId($clc_data);
        if(!$clcid){
            return $this->get_response(0,'开始学习失败');
        }

        $data['clcid'] = $clcid;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生产试卷
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function gen_paper($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps,'id,ckid,ti_num,is_repeat_ti');
        if($cl_detail['ti_num'] <= 0){
            return $this->get_response(0,'题目数量未设置');
        }
        $ckid_arr = !empty($cl_detail['ckid']) ? json_decode($cl_detail['ckid'],true) : [];
        $keys_arr = array_keys($ckid_arr);

        //获取指定题库的题目
        $maps = [];
        $maps[] = ['ckid','in',$keys_arr];
        $ct_list = model('class_ti')->get_list($maps);
        $ct_list = $ct_list->toArray();
        if(empty($ct_list)){
            return $this->get_response(0,'题库里没有题目');
        }

        //把每个库的题对应起来，例如：[1 => [1,2],2 => [3,4]]
        $ku = [];
        foreach ($ct_list as $k => $v){
            $ckid = $v['ckid'];
            if(!key_exists($ckid,$ku)){
                $ku[$ckid] = [];
            }
            array_push($ku[$ckid], $v['id']);
        }

        //获取用户已经答过的题目
        if($cl_detail['is_repeat_ti'] == 0){
            $maps = [];
            $maps[] = ['openid','=',$openid];
            $maps[] = ['clid','=',$cl_detail['id']];
            $ctids = model('class_learn_record')->get_column($maps,'ctid');
        }else{
            $ctids = [];
        }

        //生成每个题库可用的题目
        $ku_use = [];
        foreach ($ku as $k => $v) {
            $ku_use[$k] = array_diff($ku[$k],$ctids);
        }
        if(empty($ku_use)){
            return $this->get_response(0,'题库已经答完，请联系管理员补充题库');
        }

        //生成试卷
        $clp = new \app\common\controller\ClassLearnPaper();
        $paper_arr = $clp->gen_paper($openid,$cl_detail['id'],$cl_detail['ckid'],$cl_detail['ti_num'],$clcid,$ku_use);
        if(empty($paper_arr)){
            return $this->get_response(0,'题库已经学完，请联系管理员补充题库');
        }

        $r = model('class_learn_record')->insertAll($paper_arr);
        if($r){
            return $this->get_response(1,'OK');
        }
        return $this->get_response(0,'生成试卷失败');
    }

    /**
     * 获取学习题目
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function get_learn_ti($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps,'id');
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        $cl = new \app\common\controller\ClassLearn();

        $maps = [];
        $maps[] = ['clcid','=',$clcid];
        $list = model('class_learn_record')->get_list($maps);
        foreach($list as $k => $v){
            $maps = [];
            $maps[] = ['id','=',$v['ctid']];
            $ct_detail = model('class_ti')->get_detail($maps);
            //检查题型
            $ti_type = $cl->check_ti_type($ct_detail);

            $str = '&nbsp;&nbsp;&nbsp;&nbsp;' . $ct_detail['cailiao'];
            $str = str_replace('<br/>','<br/>&nbsp;&nbsp;&nbsp;', $str);

            $list[$k]['ti_type'] = $ti_type;

            $list[$k]['question'] = $ct_detail['question'];
            $list[$k]['option'] = json_decode($ct_detail['option'],true);
            $list[$k]['right_answer'] = $ct_detail['answer'];
            $list[$k]['cailiao'] = $str;
        }

        $data['list'] = $list;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查提交学习答案规则
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function check_sub_learn_anwser_rule($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps);
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps,'dati_time');

        $cp = new \app\common\controller\ClassLearn();

        $now_time = date('Y-m-d H:i:s');
        if($now_time > $clc_detail['out_time']){
            //处理超时的$c_detail 0000-00-00 00:00:00
            $r = $cp->deal_outtime_ci($clc_detail);

            //计算连续天数
            $lianxu_day = $cp->count_lianxu_day($clc_detail);

            $data['lianxu_day'] = $lianxu_day;
            return $this->get_response(0,'答题最长时间为' . $cl_detail['dati_time'] . '分钟，由于您已超时，系统已经自动提交了您的成绩',$data);
        }
        if($clc_detail['end_time'] > '0000-00-00 00:00:00'){
            return $this->get_response(0,'本试卷您已经提交过');
        }

        $data['clc_detail'] = $clc_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查预提交规则
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function check_pre_sub_rule($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps);
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps,'dati_time');

        $cp = new \app\common\controller\ClassLearn();

        $now_time = date('Y-m-d H:i:s');
        if($now_time > $clc_detail['out_time']){
            //处理超时的$c_detail 0000-00-00 00:00:00
            $r = $cp->deal_outtime_ci($clc_detail);

            //计算连续天数
            $lianxu_day = $cp->count_lianxu_day($clc_detail);

            $data['lianxu_day'] = $lianxu_day;
            return $this->get_response(0,'答题最长时间为' . $cl_detail['dati_time'] . '分钟，由于您已超时，系统已经自动提交了您的成绩',$data);
        }
        if($clc_detail['end_time'] > '0000-00-00 00:00:00'){
            return $this->get_response(0,'本试卷您已经提交过');
        }

        $data['clc_detail'] = $clc_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 预提交，计算答题对与错的数量
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function pre_sub($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps);
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        $cp = new \app\common\controller\ClassLearn();

        //处理答案，返回对与错的数量
        $answer_arr = $cp->deal_answer($clc_detail);

        $clc_data = [];
        $clc_data['right_num'] = $answer_arr['right_num'];
        $clc_data['wrong_num'] = $answer_arr['wrong_num'];

        $maps = [];
        $maps[] = ['id','=',$clc_detail['id']];
        $r = model('class_learn_ci')->force()->save($clc_data,$maps);
        if($r !== false){
            $data['right_num'] = $answer_arr['right_num'];
            $data['wrong_num'] = $answer_arr['wrong_num'];
            $data['sum_num'] = $answer_arr['right_num'] + $answer_arr['wrong_num'];
            return $this->get_response(1,'OK',$data);
        }
        return $this->get_response(0,'提交失败');
    }

    /**
     * 检查真提交规则
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function check_real_sub_rule($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps);
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        // $maps = [];
        // $maps[] = ['clcid','=',$clcid];
        // $answer_arr = model('class_learn_record')->get_column($maps,'answer');
        // if(in_array('',$answer_arr)){
        //     return $this->get_response(0,'网络不稳定，请重试');
        // }

        $maps = [];
        $maps[] = ['id','=',$clid];
        $maps[] = ['enable','=',1];
        $cl_detail = model('class_learn')->get_detail($maps,'dati_time');

        $cp = new \app\common\controller\ClassLearn();

        $now_time = date('Y-m-d H:i:s');
        if($now_time > $clc_detail['out_time']){
            //处理超时的$c_detail 0000-00-00 00:00:00
            $r = $cp->deal_outtime_ci($clc_detail);

            //计算连续天数
            $lianxu_day = $cp->count_lianxu_day($clc_detail);

            $data['lianxu_day'] = $lianxu_day;
            return $this->get_response(0,'答题最长时间为' . $cl_detail['dati_time'] . '分钟，由于您已超时，系统已经自动提交了您的成绩',$data);
        }
        if($clc_detail['end_time'] > '0000-00-00 00:00:00'){
            return $this->get_response(0,'本试卷您已经提交过');
        }

        $data['clc_detail'] = $clc_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 真提交
     * @param $openid
     * @param $clcid
     * @param $clid
     * @return mixed
     */
    public function real_sub($openid,$clcid,$clid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps);
        if(empty($clc_detail)){
            return $this->get_response(0,'非法操作');
        }

        $cp = new \app\common\controller\ClassLearn();

        Db::startTrans();
        //更新end_time
        $now_time = date('Y-m-d H:i:s');

        $maps = [];
        $maps[] = ['id','=',$clc_detail['id']];

        $clc_data = [];
        $clc_data['end_time'] = $now_time;
        $r = model('class_learn_ci')->force()->save($clc_data,$maps);
        if(!$r){
            Db::rollback();
            return $this->get_response(0,'保存失败');
        }

        //处理分数
        $r = $cp->deal_score_ci($clc_detail);
        if(!$r){
            Db::rollback();
            return $this->get_response(0,'保存失败');
        }

        //判断今天是否第一次答题
        $lianxu_day = 0;
        $r = $cp->check_first_ci($clc_detail);
        if($r){
            $lianxu_day = $cp->count_lianxu_day($clc_detail);
        }
        Db::commit();

        $data['lianxu_day'] = $lianxu_day;
        return $this->get_response(1,'OK',$data);
    }
}